﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Security;
using cognysClient.CognyService;

namespace cognysClient
{
    public partial class TMT : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// this method gets called from a client-side script to save game result data, also records value for later comparison
        /// </summary>
        /// <param name="result">object with three properties; time, faults, points</param>
        [WebMethod]
        public static void saveResult(int time, int faults, int points)
        {
            Guid person = Guid.Parse(Membership.GetUser().ProviderUserKey.ToString());

            var client = new Service1Client();
            var baseline = client.getBaseline(person);
            var resultState = client.getResultState(person);

            if (resultState.StateTMT == null)
            {

                client.editBaseLine(person, "tmt", points);

                client.editResultState(person, "tmt", 1);

            }

            else if (resultState.StateTMT == 1)
            {
                if (points > baseline.ScoreTMT)
                {
                    client.editBaseLine(person, "tmt", points);
                }
                client.editResultState(person, "tmt", 2);
            }
            else
            {
                ResultTMT tmt = new ResultTMT();
                tmt.Person = Guid.Parse(Membership.GetUser().ProviderUserKey.ToString());
                tmt.regDate = DateTime.Now;
                tmt.compTime = time;
                tmt.Error = faults;
                tmt.Score = points;
                client.addTmtResult(tmt);
            }             
        }
    }
}